package com.demo.demo.images;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.net.URL;

/**
 *  合成图片
 *
 * @Author WuHuang
 * @Date 2023/5/10
 */

public class ImageProcessor {

    public static void main(String[] args) throws IOException {
        Thumbnails.of("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\output3.png").scale(1).outputQuality(0.3).toFile("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\" + "qiwei/test1.jpg");
    }
//    public static void main(String[] args) {
//        try {
//            // Load the input image
//            File file = new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\619762ba2cace5236.jpg");
//            URL url = new URL("https://pic.616pic.com/photoone/00/07/29/619762ba2cace5236.jpg");
//            // Convert multipartFile to File
////            MultipartFile multipartFile = null;// get the uploaded file from frontend
////            File file = new File(multipartFile.getOriginalFilename());
////            multipartFile.transferTo(file);
//
//            BufferedImage inputImage = ImageIO.read(url);
//            // Create a graphics object for the input image
//            Graphics2D g = inputImage.createGraphics();
//
//            // Set the font and color for the price text
//            Font font = new Font("宋体", Font.BOLD, 24);//默认 24
//            Color textColor1 = Color.WHITE;
//            Color textColor2 = Color.BLACK;
//            Color bgColor = new Color(217, 0, 27);
//            Color bgColor2 = new Color(255, 255, 255);
//            g.setFont(font);
//
//            // Draw the first price label with red background at top right corner
//            String price1 = "活动价: ¥102.99";
//            //横坐标
//            int x1 = inputImage.getWidth() - g.getFontMetrics().stringWidth(price1) - 20;
//            //纵坐标
//            int y1 = 35;
//            //标签的宽度
//            int width1 = g.getFontMetrics().stringWidth(price1) + 40;
//            //标签的高度
//            int height1 = 60;
//            g.setColor(bgColor);
//            g.fillRect(x1 - 18, y1 - 50, width1, height1);
//            g.setColor(textColor1);
//            g.drawString(price1, x1 + 5, y1 - 3);
//
//            // Draw the second price label with strike-through effect at top right corner
//            String price2 = "吊牌价: ¥122.99";
//            //横坐标
//            int x2 = x1;
//            //纵坐标
//            int y2 = y1 + 50;
//            g.setColor(bgColor2);
//            g.fillRect(x2 - 18, y2 - 40, width1, height1-20);
//            g.setColor(textColor2);
//            g.drawString(price2 , x2 + 5, y2 - 3);
//            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
//            g.drawLine(x2 - 10, y2 - 10, x2 + g.getFontMetrics().stringWidth(price2) + 5, y2 - 20);
//
//            // Save the modified image to a file
//            ImageIO.write(inputImage, "png", new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\output10.jpg"));
//            // Dispose of the graphics object
//            g.dispose();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


    //处理图片空方法
    public void imageSynthesis(String imagesUrl,String price1, String price2){
        try {
            // Load the input image
//            File file = new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\619762ba2cace5236.jpg");
            URL url = new URL("https://pic.616pic.com/photoone/00/07/29/619762ba2cace5236.jpg");

            MultipartFile multipartFile = null;// get the uploaded file from frontend
            // Convert multipartFile to File
            File file = new File(multipartFile.getOriginalFilename());
            multipartFile.transferTo(file);
            BufferedImage inputImage = ImageIO.read(file);

            // Create a graphics object for the input image
            Graphics2D g = inputImage.createGraphics();

            // Set the font and color for the price text
            Font font = new Font("Arial", Font.BOLD, 24);
            Color textColor = Color.WHITE;
            Color bgColor = new Color(255, 0, 0, 128);
            g.setFont(font);
            // Draw the first price label with red background at top right corner
            //横坐标
            int x1 = inputImage.getWidth() - g.getFontMetrics().stringWidth(price1) - 20;
            //纵坐标
            int y1 = 30;
            //标签的宽度
            int width1 = g.getFontMetrics().stringWidth(price1) + 10;
            //标签的高度
            int height1 = 30;
            g.setColor(bgColor);
            g.fillRect(x1, y1 - height1, width1, height1);
            g.setColor(textColor);
            g.drawString(price1, x1 + 5, y1 - 5);

            // Draw the second price label with strike-through effect at top right corner
            //横坐标
            int x2 = x1;
            //纵坐标
            int y2 = y1 + 30;
            g.setColor(textColor);
            g.drawString(price2, x2 + 5, y2 - 5);
            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            g.drawLine(x2 + 5, y2 - 13, x2 + g.getFontMetrics().stringWidth(price2) + 5, y2 - 13);

            // Save the modified image to a file
            ImageIO.write(inputImage, "png", new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\output7.jpg"));

            // Dispose of the graphics object
            g.dispose();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }





}

